﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Reflection;
using ApplicationGenerator.CommonModule;
using System.IO;

namespace WorkflowLibrary.Activities
{
    [Designer(typeof(ExecuteCodeActivityDesigner))]
    public partial class ExecuteCodeActivity : Activity, IActivityName//, ICustomTypeDescriptor
    {
        public ExecuteCodeActivity()
        {
            InitializeComponent();
            ActivityName = "Wykonaj kod";
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Assembly destAssembly = Assembly.LoadFrom(Path.Combine(@"D:\studia\Magisterka\ApplicationGenerator\ApplicationGeneratorWeb\Bin\", (AssemblyName.Contains(".dll")) ? AssemblyName : AssemblyName + ".dll"));
            Type destType = destAssembly.GetType(ClassName);
            ICustomCode customCode = Activator.CreateInstance(destType) as ICustomCode;
            customCode.ElementId = (Parent as GeneratorSequentialWorkflow).InstanceId;
            customCode.ExecuteCode();

            return ActivityExecutionStatus.Closed;
        }

        #region Dependency Properties

        public static DependencyProperty AssemblyNameProperty =
            DependencyProperty.Register("AssemblyName",
            typeof(string),
            typeof(ExecuteCodeActivity));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [DescriptionAttribute("Nazwa pliku dll, w której znajduje się klasa, której kod ma zostać wykonany")]
        [DisplayName("Nazwa pliku dll")]
        [CategoryAttribute("Dane rozszerzone")]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public string AssemblyName
        {
            get
            {
                return (string)(base.GetValue(ExecuteCodeActivity.AssemblyNameProperty));
            }
            set
            {
                base.SetValue(ExecuteCodeActivity.AssemblyNameProperty, value);
            }
        }

        public static DependencyProperty ClassNameProperty =
            DependencyProperty.Register("ClassName",
            typeof(string),
            typeof(ExecuteCodeActivity));

        [DesignerSerializationVisibilityAttribute
            (DesignerSerializationVisibility.Visible)]
        [DescriptionAttribute("Nazwa klasy implementującej ICustomCode")]
        [DisplayName("Nazwa klasy")]
        [CategoryAttribute("Dane rozszerzone")]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public string ClassName
        {
            get
            {
                return (string)(base.GetValue(ExecuteCodeActivity.ClassNameProperty));
            }
            set
            {
                base.SetValue(ExecuteCodeActivity.ClassNameProperty, value);
            }
        }
        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);
            colDisplayProperties.Add(colAvailableCollection["AssemblyName"]);
            colDisplayProperties.Add(colAvailableCollection["ClassName"]);

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members
        [Category("Dane podstawowe")]
        [DisplayName("Nazwa czynności")]
        [Description("Nazwa czynności przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion
    }
}
